// you can use includes, for example:
// #include <algorithm>
#include <unordered_map>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;

string solution(string &S) {
    // write your code in C++14 (g++ 6.2.0)
    unordered_map<char, int> filter_num;
    filter_num['a'] = 0;
    filter_num['b'] = 0;
    filter_num['?'] = 0;


    int right = 3;
    for(int i = 0; i <S.size(); ) {
   
        if(S[i] == '?'){
            if(filter_num['a'] == 2){
                S[i] = 'b';
            }
            else if(filter_num['b'] == 2){
                S[i] = 'a';
            }
        }

        filter_num[S[i]]++;
        i++;
        if(i >= right){
            filter_num[S[i-3]]--;
        }
    }

    filter_num['a'] = 0;
    filter_num['b'] = 0;
    filter_num['?'] = 0;
    right =  S.size()-1-3;
    for(int i = S.size()-1; i >=0; ) {

        if(S[i] == '?'){
            if(filter_num['a'] == 2){
                S[i] = 'b';
            }
            else if(filter_num['b'] == 2){
                S[i] = 'a';
            }
            else if(filter_num['a'] <= 1 && filter_num['b'] <=1){
                S[i] = 'a';
            }
        }

        filter_num[S[i]]++;
        i--;
        if(i <= right){
            filter_num[S[i+3]]--;
            right--;
        }
    }
    return S;
}



// you can use includes, for example:
// #include <algorithm>

// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
void get_similarity(vector<string>& cars, vector<int>& res, int i, int j){
    int unsimilar_nums = 0;
    for(int k=0; k<cars[i].size(); k++){
        if(cars[i][k] != cars[j][k]){
            unsimilar_nums++;
        }
        if(unsimilar_nums > 1) break;
    }
    if(unsimilar_nums <=1 ){
        res[i] ++;
        res[j] ++;
    }
}

vector<int> solution(vector<string> &cars) {
    // write your code in C++14 (g++ 6.2.0)
    vector<int> result(cars.size(), 0);

    for(int i = 0; i < cars.size()-1; i++){
        for(int j = i+1; j < cars.size(); j++){
            get_similarity(cars, result, i, j);
        }
    }

    return result;
}



// you can use includes, for example:
// #include <algorithm>
#include <algorithm>
#include <unordered_map>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;

int solution(vector<int> &A) {
    // write your code in C++14 (g++ 6.2.0)
    unordered_map<int, int> num_map;
    int max_num = 0;
    for(int i=0; i<A.size(); i++){
        num_map[A[i]]++;
        num_map[A[i]] = min(num_map[A[i]], 2);
        max_num = max(max_num, A[i]);
    }
    num_map[max_num] = min(num_map[A[i]], 1);

    int length = 0;
    for(auto it=num_map.begin(); it!=num_map.end(); it++){
        length += it->second;
    }
    return length;
}

